

import java.net.URL;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import org.apache.soap.Header;
import org.apache.soap.Fault;
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.transport.http.SOAPHTTPConnection;
import org.apache.soap.util.xml.QName;
import org.apache.soap.Constants;
import org.apache.soap.encoding.soapenc.StringDeserializer;
import org.apache.soap.encoding.soapenc.BeanSerializer;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import org.apache.soap.SOAPException;
/**
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
*/

public class encode {

	/**
	 * @param args
	 */
	  public static void main(String args[]) throws MalformedURLException, SOAPException {
		// TODO Auto-generated method stub
	   // String encoding = System.getProperty("file.encoding"); 
	   // System.out.println(encoding); 
	    SOAPHTTPConnection st = new SOAPHTTPConnection();
	    String  str_url = "http://localhost/webservice2/Service.asmx";//"http://www.wopos.com/webservice/chengyu.asmx";
	    String methodname = "HelloWorld";  // "ChengYuDianGu";//
	    String namespace = "http://first/";//"http://tempuri.org/" ;
	    URL url = new URL(str_url);
        // 参数名
        String name = "laiheng";
        Call call = new Call() ;//new Call();
        call.setTargetObjectURI(namespace);
        // 下面为方法名
        call.setMethodName(methodname);
        call.setHeader(new Header());
        call.setSOAPTransport(st);
        call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);//"http://schemas.xmlsoap.org/soap/encoding/");//(Constants.NS_URI_SOAP_ENC);
        
     //   call.setEncodingStyleURI("http://schemas.xmlsoap.org/soap/encoding/");//(Constants.);
        //***************
		  SOAPMappingRegistry sm = new SOAPMappingRegistry();
		  StringDeserializer sd = new StringDeserializer();
		  BeanSerializer bd = new BeanSerializer();
		  sm.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(namespace, methodname+ "Result"), String.class, null,sd);
		  sm.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(namespace,"name"), String.class, bd, bd);
		  call.setSOAPMappingRegistry(sm);
		  
		  //*******************		  
         Vector params = new Vector();
      //  params.addElement(new Parameter("ChengYu", String.class, "fjqz", null));
        params.add(new Parameter("name", String.class, new String("hujun"), null));
        call.setParams(params);
        
        System.out.print("远程的服务器对你说: ");
        Response resp = call.invoke(url, namespace + methodname);
        params = resp.getParams();
        if (resp.generatedFault()) {
               Fault fault = resp.getFault();
               System.out.println("\nOuch, the call failed: ");
               System.out.println(" Fault Code = " + fault.getFaultCode());
               System.out.println(" Fault String = " + fault.getFaultString());
        } else {
               Parameter result = resp.getReturnValue();
               System.out.print(result.getValue());
               System.out.println();
        }

	}

}
